# Definition for a binary tree node.
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None


class Solution:
    def flatten(self, root: TreeNode) -> None:
        """
        Do not return anything, modify root in-place instead.
        """
        self.mid_list = []
        if root:
            self.search(root)
        self.mid_list.append(None)
        for i in range(len(self.mid_list)-1):
            self.mid_list[i].left = None
            self.mid_list[i].right = self.mid_list[i+1]

    def search(self, root: TreeNode):
        self.mid_list.append(root)
        if root.left:
            self.search(root.left)
        if root.right:
            self.search(root.right)
